#--1---    -----2----       ----3---
#1gender    first_name       last_name
#2address   city             state
#3zip_code  email            primary_phone
#4birthdate marital_status   make
#5year      model            proxy
import win32com.client
import os
import random
import time
#userinfo
header = []
data   = []
#referer.txt
referer_list = []
#url.txt
url_list= []
def SetImData(e,w):
    for idx,ee in enumerate(e):
        varname = str(idx)
        if idx == 13:
            model = ee.replace(' ','<SP>')
                #w.iimSet(varname,'A4<SP>3.0<SP>CABRIOLET')
            w.iimSet(varname,ee)
                #print(model)
        elif idx == 0:
            gender = ee[0]
            w.iimSet(varname,gender)
        elif idx == 8:
            home_phone = ee.split('-')
            home1 = home_phone[0]
            home2 = home_phone[1]
            home  = home_phone[2]
            w.iimSet("home1",home1)
            w.iimSet("home2",home2)
            w.iimSet("home",home)
                #print(home1,home2,home)
        else:
            w.iimSet(varname,ee)
def Main():
    import win32com.client
    w=win32com.client.Dispatch("imacros")
    useri = open('./userinfo.txt')
    for k, v in enumerate(useri):
        if k == 0:
            header = v[:-1].split('\t')
        else:
            data.append(v[:-1].split('\t'))

    referer_f = open('./referer.txt','r')
    for k, v in enumerate(referer_f):
        referer_list.append(v[:-1])

    url_f = open('./url.txt','r')
    for k,v in enumerate(url_f):
        url_list.append(v[:-1])
    #for e in data:
    e = data[0]
    if e:
        #set proxy
        proxy = e[14].upper()
        print('region:',proxy)
        os.system('autoproxy.exe '+proxy)
        #set referer
        time.sleep(5)
        index = random.randint(0, len(referer_list)-1)
        print(url_list[0])
        os.system('spoof.exe '+referer_list[index]+' '+url_list[0])
        print(index,referer_list[index],url_list[0],e[6])
        #run macro
        w.iimOpen("-fx",0)
        SetImData(e,w)
        w.iimPlay("IIMScripte\\carinsure.iim")
        while 1:
            url = w.iimGetLastExtract()
            print('urlmessage:',url)
            #if url.find("policypagesingleaction") != -1:
            #elif url.find("vehiclepagesingleaction") != -1:
            if url == 'http://www.car-insure.org/auto/policypagesingleaction.action':
                SetImData(e,w)
                w.iimPlay("IIMScripte\\policypagesingleaction.iim")
            #personal info
            elif url == 'http://www.car-insure.org/auto/vehiclepagesingleaction.action':
                w.iimPlay("IIMScripte\\vehiclepagesingleaction.iim")
            else:
                break
if __name__=='__main__':
    Main()
